// SPDX-License-Identifier: GPL-2.0
#ifndef _NF_CT_EXPAND_H
#define _NF_CT_EXPAND_H

#include <net/netfilter/nf_conntrack_expect.h>
#include <net/netfilter/nf_nat.h>

#define MAP_DEVICE_NAME "map-e.1"

#ifdef CONFIG_ONT_NET_FEATURES

extern void (*nf_ct_sip_expect_hook)(struct nf_conn *ct);

struct nf_conntrack_expect *
nf_ct_expect_save_proto_find(const struct nf_conntrack_tuple *tuple);
struct nf_conntrack_expect *
nf_conntrack_expect_find(struct net *net, const struct nf_conntrack_expect *a);
struct nf_conn *nf_conntrack_find_src_by_wanip(const struct nf_conntrack_tuple *tuple,
					       const union nf_inet_addr *addr);
int nf_ct_expect_refresh_timer(struct nf_conntrack_expect *exp);
int nf_ct_expect_exist(struct nf_conntrack_expect *exp, unsigned int flags);
bool nf_skip_nat_port(struct nf_conntrack_tuple *tuple, struct nf_conn *ct);
bool nf_check_map_tunnel(const struct sk_buff *skb, const struct nf_conn *ct);
bool nf_check_map_port_valid(const char *dev_name, u16 port, u32 rsvd_num);
bool nf_check_conenat_used(void);
bool nf_check_nat_port_inuse(struct nf_conntrack_tuple *tuple, struct nf_conn *ct, int rsvd_num);

void nf_set_session_dbg_mark(struct sk_buff *skb, unsigned int bit);
void nf_set_sip_noacc_rule(unsigned int addr, unsigned short port);
void nf_log_ipv4_packet(struct net *net, u8 pf, unsigned int hooknum,
			const struct sk_buff *skb, const struct net_device *in,
			const struct net_device *out, const void *loginfo,
			const char *prefix);
void nf_log_ipv6_packet(struct net *net, u8 pf, unsigned int hooknum,
			const struct sk_buff *skb, const struct net_device *in,
			const struct net_device *out, const void *loginfo,
			const char *prefix);

void nf_nat_masquerade_dbg_init(void);
int nf_nat_masquerade_tg_check(const struct xt_tgchk_param *par);
unsigned int nf_nat_masquerade_ipv4_ext(struct sk_buff *skb, unsigned int hooknum,
					const struct nf_nat_range2 *range,
					const struct net_device *out,
					unsigned int rangesize);

static inline void nf_nat_masquerade_set_range_addr(struct nf_nat_range2 *range2,
						    const struct nf_nat_ipv4_range *range)
{
	memset(&range2->min_addr, 0, sizeof(range2->min_addr));
	memset(&range2->max_addr, 0, sizeof(range2->max_addr));
	range2->min_addr.ip = range->min_ip;
	range2->max_addr.ip = range->max_ip;
}

#define NF_CT_MARK_ALG_SIP 0x10000000
static inline void nf_nat_mark_alg_sip(struct nf_conn *ct)
{
	ct->mark |= NF_CT_MARK_ALG_SIP;
}

u16 nf_nat_adapt_ftp_port(struct sk_buff *skb, struct nf_conntrack_expect *exp, int dir);
int nf_nat_adapt_sip_port(struct nf_conntrack_expect *exp, u16 *port, bool *exp_related);
int nf_nat_adapt_rtp_port(struct nf_conntrack_expect *rtp_exp,
			  struct nf_conntrack_expect *rtcp_exp,
			  u16 *port, bool *exp_related);

#else /* CONFIG_ONT_NET_FEATURES */

static inline void nf_set_session_dbg_mark(struct sk_buff *skb, unsigned int bit)
{
}

static inline void nf_set_sip_noacc_rule(unsigned int addr, unsigned short port)
{
}

static inline void
nf_log_ipv4_packet(struct net *net, u8 pf, unsigned int hooknum,
		   const struct sk_buff *skb, const struct net_device *in,
		   const struct net_device *out, const void *loginfo,
		   const char *prefix)
{
}

static inline void
nf_log_ipv6_packet(struct net *net, u8 pf, unsigned int hooknum,
		   const struct sk_buff *skb, const struct net_device *in,
		   const struct net_device *out, const void *loginfo,
		   const char *prefix)
{
}

static inline void nf_nat_masquerade_dbg_init(void)
{
}

static inline int nf_nat_masquerade_tg_check(const struct xt_tgchk_param *par)
{
	return 0;
}

static inline unsigned int
nf_nat_masquerade_ipv4_ext(struct sk_buff *skb, unsigned int hooknum,
			   const struct nf_nat_range2 *range,
			   const struct net_device *out,
			   unsigned int rangesize)
{
	return NF_ACCEPT;
}

static inline void nf_nat_masquerade_set_range_addr(struct nf_nat_range2 *range2,
						    const struct nf_nat_ipv4_range *range)
{
}

static inline void nf_nat_mark_alg_sip(struct nf_conn *ct)
{
}

static inline bool nf_check_conenat_used(void)
{
	return false;
}

static inline bool nf_check_map_port_valid(const char *dev_name, u16 port, u32 rsvd_num)
{
	return true;
}

static inline u16
nf_nat_adapt_ftp_port(struct sk_buff *skb, struct nf_conntrack_expect *exp, int dir)
{
	return 0;
}

static inline int
nf_nat_adapt_sip_port(struct nf_conntrack_expect *exp, u16 *port, bool *exp_related)
{
	return 0;
}

static inline int
nf_nat_adapt_rtp_port(struct nf_conntrack_expect *rtp_exp, struct nf_conntrack_expect *rtcp_exp,
		      u16 *port, bool *exp_related)
{
	return 0;
}

#endif /* CONFIG_ONT_NET_FEATURES */

#endif /* _NF_CT_EXPAND_H */
